#include "updatemorearticlelistresponse.h"
#include "newspaper/article.h"

#include <QDebug>

UpdateMoreArticleListResponse::UpdateMoreArticleListResponse(QObject *parent) :
    JSONResponse(parent),
    m_checkReplyDataSuccess(false)
{
}


// ---------------------------------------------------------------------------------
Network::Response::Type UpdateMoreArticleListResponse::type() const
{
    return Response::UpdateMoreListArticles;
}

// ---------------------------------------------------------------------------------
bool UpdateMoreArticleListResponse::parseSid(const QVariantMap &map)
{
    if (!map.contains("sid")) {
        setError(Response::Property, "The \"sid\" value missing");
        return false;
    }
    QVariant sid = map.value("sid");
    if (sid.type() != QVariant::Int) {
        setError(Response::Property, "The \"status\" value isn't competitived");
        return false;
    }

    m_idSource = sid.toInt();

    return true;
}

// ---------------------------------------------------------------------------------
bool UpdateMoreArticleListResponse::parseCid(const QVariantMap &map)
{
    if (!map.contains("cid")) {
        setError(Response::Property, "The \"cid\" value missing");
        return false;
    }
    QVariant cid = map.value("cid");
    if (cid.type() != QVariant::Int) {
        setError(Response::Property, "The \"cid\" value isn't competitived");
        return false;
    }

    m_idCategory = cid.toInt();

    return true;
}

// ---------------------------------------------------------------------------------
bool UpdateMoreArticleListResponse::parseListArticleId(const QVariantMap &map)
{
    if (!map.contains("lids")) {
        setError(Response::Property, "The \"lids\" value missing");
        return false;
    }
    QVariant lids = map.value("lids");
    if (lids.type() != QVariant::List) {
        setError(Response::Property, "The \"lids\" value isn't competitived");
        return false;
    }

    QStringList listArticleId = lids.toStringList();

    for (int i = 0; i < listArticleId.length(); i++) {

        m_listArticleId.append(listArticleId.at(i).toLongLong());
    }

    return true;
}

// ---------------------------------------------------------------------------------
bool UpdateMoreArticleListResponse::parseListArticle(const QVariantMap &map)
{
    if (!map.contains("linfos")) {
        setError(Response::Property, "The \"linfos\" value missing");
        return false;
    }
    QVariant linfos = map.value("linfos");
    if (linfos.type() != QVariant::List) {
        setError(Response::Property, "The \"linfos\" value isn't competitived");
        return false;
    }

    QVariantList listArticleInfo = linfos.toList();

    foreach (QVariant item, listArticleInfo) {

        if (item.type() != QVariant::Map) {
            setError(Network::Response::Property, QString("The \"map\" field isn't a map"));
            return false;
        }

        QVariantMap mapChild = item.toMap();

        if (!mapChild.contains("sid")) {
            setError(Response::Property, "The \"sid\" value missing");
            return false;
        }
        QVariant sid = mapChild.value("sid");
        if (sid.type() != QVariant::Int) {
            setError(Response::Property, "The \"sid\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("lid")) {
            setError(Response::Property, "The \"lid\" value missing");
            return false;
        }
        QVariant lid = mapChild.value("lid");
        if (lid.type() != QVariant::Int) {
            setError(Response::Property, "The \"lid\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("title")) {
            setError(Response::Property, "The \"title\" value missing");
            return false;
        }
        QVariant title = mapChild.value("title");
        if (title.type() != QVariant::String) {
            setError(Response::Property, "The \"title\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("desc")) {
            setError(Response::Property, "The \"desc\" value missing");
            return false;
        }
        QVariant desc = mapChild.value("desc");
        if (desc.type() != QVariant::String) {
            setError(Response::Property, "The \"desc\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("thumbnailAspectRatio")) {
            setError(Response::Property, "The \"thumbnailAspectRatio\" value missing");
            return false;
        }
        QVariant thumbnailAspectRatio = mapChild.value("thumbnailAspectRatio");
        if (thumbnailAspectRatio.type() != QVariant::Double &&
                thumbnailAspectRatio.type() != QVariant::Int) {
            setError(Response::Property, "The \"thumbnailAspectRatio\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("posttime")) {
            setError(Response::Property, "The \"posttime\" value missing");
            return false;
        }
        QVariant posttime = mapChild.value("posttime");
        if (posttime.type() != QVariant::Int) {
            setError(Response::Property, "The \"posttime\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("image")) {
            setError(Response::Property, "The \"image\" value missing");
            return false;
        }
        QVariant image = mapChild.value("image");
        if (image.type() != QVariant::String) {
            setError(Response::Property, "The \"image\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("small_thumbnail")) {
            setError(Response::Property, "The \"small_thumbnail\" value missing");
            return false;
        }
        QVariant small_thumbnail = mapChild.value("small_thumbnail");
        if (small_thumbnail.type() != QVariant::String) {
            setError(Response::Property, "The \"small_thumbnail\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("cid")) {
            setError(Response::Property, "The \"cid\" value missing");
            return false;
        }
        QVariant cid = mapChild.value("cid");
        if (cid.type() != QVariant::Int) {
            setError(Response::Property, "The \"cid\" value isn't competitived");
            return false;
        }

        if (!mapChild.contains("url")) {
            setError(Response::Property, "The \"url\" value missing");
            return false;
        }
        QVariant url = mapChild.value("url");
        if (url.type() != QVariant::String) {
            setError(Response::Property, "The \"url\" value isn't competitived");
            return false;
        }

        Article* article = new Article(sid.toInt(),
                                       lid.toInt(),
                                       title.toString(),
                                       desc.toString(),
                                       thumbnailAspectRatio.toDouble(),
                                       posttime.toInt(),
                                       image.toString(),
                                       small_thumbnail.toString(),
                                       cid.toInt(),
                                       url.toString());
        m_listArticle.append(article);
    }


    return true;
}

// ---------------------------------------------------------------------------------
void UpdateMoreArticleListResponse::parse(const QVariant &data)
{
    //qDebug() << data;
    if (data.type() != QVariant::Map){
        setError(Response::Property, "The error code field isn't competitived");
        return;
    }

    QVariantMap map = data.toMap();

    if (!parseSid(map)) {
        return;
    }

    if (!parseCid(map)) {
        return;
    }

    if (!parseListArticleId(map)) {
        return;
    }

    if (!parseListArticle(map)) {
        return;
    }

    m_checkReplyDataSuccess = true;
}
